/*
 *  SPAGL Copyright (C) 2008 Sam Pottinger
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser 
 *  General Public License as published by the Free Software Foundation; either version 2.1 of the License, or 
 *  (at your option) any later version. This library is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not, 
 *  write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package spagswt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Combo;
import spagcore.SpaglCoreContainer;
import spagcore.SpaglCoreEvent;
import spagcore.SpaglCoreFont;
import spagcore.SpaglCoreCombo;
import spagswt.SpaglContainer;

/**
 * A Combo Box (org.eclipse.swt.widgets.Combo) that conforms to the SpaglCoreList interface
 * @author Sam Pottinger
 */
public class SpaglCombo implements SpaglSwtWidget, SpaglCoreCombo {
	
	private Combo innerWidget;
	private SpaglCoreContainer parentWidget;
	private SpaglCoreFont widgetFont;
	
	public static int SINGLE = SWT.SINGLE;
	public static int MULTI = SWT.MULTI;
	
	/**
	 * Creates a bare bone/empty combo box
	 * @param newParent The parent {@link SpaglContainer} for this widget
	 */
	public SpaglCombo(SpaglCoreContainer newParent)
	{
		innerWidget = new Combo(((SpaglSwtContainer)newParent).getComposite(), SWT.BORDER);
		parentWidget = newParent;
	}
	
	/**
	 * Creates a combo box with specified placement parameters for the layout manager
	 * 
	 * @param newParent The parent {@link SpaglContainer} for this widget
	 * @param placementParameters The placement parameters for the layout manager
	 */
	public SpaglCombo(SpaglCoreContainer newParent, String placementParameters)
	{
		innerWidget = new Combo(((SpaglSwtContainer)newParent).getComposite(), SWT.BORDER);
		parentWidget = newParent;
		parentWidget.placeWidget(this, placementParameters);
	}

	/**
	 * Gets the native Combo Box
	 * 
	 * @return The native SWT widget for this object
	 */
	public Control getNativeWidget() {
		return innerWidget;
	}

	/**
	 * Gets the parent container for the object
	 * 
	 * @return The {@link SpaglContainer} for this object as a {@link SpaglCoreContainer}
	 */
	public SpaglCoreContainer getContainer() {
		return parentWidget;
	}

	/**
	 * Gets the height in pixels of the object
	 * 
	 * @return Gets the height
	 */
	public int getHeight() {
		return innerWidget.getBounds().y;
	}

	/**
	 * Gets the width in pixels of the object
	 * 
	 * @return The native width of this widget
	 */
	public int getWidth() {
		return innerWidget.getBounds().x;
	}

	/**
	 * Sets the size of the object in pixels
	 * 
	 * @param width the horizontal size in size x
	 * @param height the vertical size in size y
	 */
	public void setSize(int width, int height) {
		innerWidget.setSize(width, height);
	}

	/**
	 * Adds an element to the combo box
	 * 
	 * @param newElement the new object to be added
	 */
	public void addElement(Object newElement) {
		innerWidget.add(newElement.toString());
	}

	/**
	 * Although slighly redundant, this is a java doc so... this clears the selection on the widget
	 *
	 */
	public void clearSelection() {
		innerWidget.deselectAll();
	}

	/**
	 * Get the index of the of the selected object
	 * 
	 * @return the index of the choice selected by the user
	 */
	public int getSelectedIndex() {
		return innerWidget.getSelectionIndex();
	}

	/**
	 * Removes all elements from this combo box
	 */
	public void removeAllElements() {
		innerWidget.removeAll();
	}

	/**
	 * Removes an element at a given position
	 * 
	 * @param i the index of the element to remove
	 */
	public void removeElement(int i) {
		innerWidget.remove(i);
	}

	/**
	 * Sets the combo box to a given array of objects
	 * 
	 * @param newElementList an array of objects that will be associated with this widget
	 */
	public void setElementList(Object[] newElementList) {
		String[] newStringList = new String[newElementList.length-1];
		for(int i = 0; i<newElementList.length; i++)
			newStringList[i] = newElementList[i].toString();
		innerWidget.setItems(newStringList);
	}

	/**
	 * Set the choosen object to the selected index
	 * 
	 * @param i the index to be selected
	 */
	public void setSelectedIndex(int i) {
		innerWidget.setSelection(new Point(i, i));
	}

	/**
	 * Gets the widget's font
	 * 
	 * @return The {@link SpaglFont} associated with the object as a {@link SpaglCoreFont}
	 */
	public SpaglCoreFont getWidgetFont() {
		return widgetFont;
	}

	/**
	 * Sets the font for this widget
	 * 
	 * @param newFont a {@link SpaglFont} to associate with the object
	 */
	public void setFont(SpaglCoreFont newFont) {
		widgetFont = newFont;
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
	}
	
	/**
	 * Sets the font to be attached with this combo box while simultating disposal
	 * 
	 * @param newFont the font to be attached with this combo box
	 * @param dispose This is ignored by swing, for cross library purposes this would (if true) dipose the resource instantly
	 */
	public void setFont(SpaglCoreFont newFont, boolean dispose) {
		innerWidget.setFont(((SpaglFont)newFont).getNativeFont());
		if(dispose)
		{
			widgetFont = null;
			((SpaglFont)newFont).dispose();
		}
		else
			widgetFont = newFont;
	}
	
	/**
	 * Disposes this widget through SWT
	 */
	public void dispose()
	{
		innerWidget.dispose();
	}
	
	/**
	 * Adds an event listener to this check box
	 * 
	 * @param eventID The ID of the event to be listened for
	 * @param method The (string) name of the method to be called when the event is fired
	 * @param o The object where the method should be fired
	 */
	public void addEventListener(int eventID, String method, Object o) {
		try{
			switch(eventID)
			{
				case SpaglEvent.SELECTION_CHANGED_EVENT: {innerWidget.addListener(SWT.Selection, new SpaglEventListener(o.getClass().getMethod(method, SpaglCoreEvent.class), o, SpaglEvent.SELECTION_EVENT)); break;}
				default: throw new IllegalArgumentException("Event not supported by this widget");
			}}
		catch (SecurityException e) {e.printStackTrace();} 
		catch (NoSuchMethodException e) {e.printStackTrace();}
		
	}
}
